import {config} from "@/config/config"

const baseURL = config.baseURL
const httpInterceptor = {
  invoke(options: UniApp.RequestOptions) {
    if (!options.url.startsWith("http")) {
      options.url = baseURL + options.url;
    }
    options.timeout = 10000;
    options.header = {
      ...options.header,
      // "Content-Type": "application/json; charset=utf-8",  // json
      "Content-Type": "application/x-www-form-urlencoded",   // form-data
    };
    const token = uni.getStorageSync("token");
    if (token) {
      options.header.token = token;
    }
  },
};
uni.addInterceptor("request", httpInterceptor);

const uploadInterceptor = {
  invoke(options: UniApp.UploadFileOption) {
    if (!options.url.startsWith("http")) {
      options.url = baseURL + options.url;
    }
    options.name = "file";
    options.timeout = 10000;
    options.header = {
      Authorization: "Bearer " + uni.getStorageSync("token"),
    };
  },
};
uni.addInterceptor("uploadFile", uploadInterceptor);

interface Data<T> {
  code: number;
  msg: string;
  time: string;
  data: T;
  rows?: T;
  total?: number;
}
export const http = <T>(options: UniApp.RequestOptions) => {
  return new Promise<Data<T>>((resolve, reject) => {
    uni.request({
      ...options,
      success(res) {
        if (res.statusCode >= 200 && res.statusCode < 300) {
          let info = res.data as Data<T>;
          if (info.code !== 200) {
            uni.showToast({
              title: info.msg || "请求错误",
              icon: "none",
              mask: true,
              duration: 1000,
            });
            if (info.code == 401) {
              uni.navigateTo({
                url: "/pages/login/login",
              });
            }
          }
          resolve(res.data as Data<T>);
        } else if (res.statusCode === 401) {
          uni.showToast({
            title: (res.data as Data<T>).msg || "请求错误",
            icon: "none",
            mask: true,
            duration: 1000,
          });
          // uni.navigateTo({
          //   url: '/pages/login/index',
          // })
          reject(res);
        } else {
          uni.showToast({
            title: (res.data as Data<T>).msg || "请求错误",
            icon: "none",
          });
          reject(res);
        }
      },
      fail(err) {
        uni.showToast({
          title: "网络错误，换个网络试试",
          icon: "none",
        });
        reject(err);
      },
    });
  });
};
/** 文件 */
interface uploadData {
  url: string;
  /** 链接 */
  fullurl: string;
}
export const upload = (options: UniApp.UploadFileOption) => {
  return new Promise<Data<uploadData>>((resolve, reject) => {
    uni.uploadFile({
      ...options,
      success(res) {
        if (res.statusCode >= 200 && res.statusCode < 300) {
          resolve(JSON.parse(res.data));
        } else if (res.statusCode === 401) {
          uni.showToast({
            title: "请求错误",
            icon: "none",
            mask: true,
            duration: 1000,
          });
          uni.navigateTo({
            url: "/pages/login/index",
          });
          reject(res);
        } else {
          uni.showToast({
            title: "请求错误",
            icon: "none",
          });
          reject(res);
        }
      },
      fail(err) {
        uni.showToast({
          title: "网络错误，换个网络试试",
          icon: "none",
        });
        reject(err);
      },
    });
  });
};
